package com.yida.gwj.config.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.gwj.api.domain.GwjMaintenanceFundsBusinessInner;
import com.yida.gwj.api.service.RemoteGwjExpendInnerService;
import com.yida.gwj.config.domain.dto.GwjConfNoticeDetailDTO;
import com.yida.gwj.config.domain.dto.GwjConfNoticePageDTO;
import com.yida.gwj.config.domain.request.GwjConfNoticeSaveRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yida.gwj.config.mapper.GwjConfNoticeMapper;
import com.yida.gwj.config.domain.GwjConfNotice;
import com.yida.gwj.config.service.IGwjConfNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公维金信息公示Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-02
 */
@Service
public class GwjConfNoticeServiceImpl implements IGwjConfNoticeService 
{
    @Autowired
    private GwjConfNoticeMapper gwjConfNoticeMapper;

    @Autowired
    private RemoteGwjExpendInnerService remoteGwjExpendInnerService;

    /**
     * 查询公维金信息公示
     * 
     * @param id 公维金信息公示主键
     * @return 公维金信息公示
     */
    @Override
    public GwjConfNoticeDetailDTO selectGwjConfNoticeById(Long id)
    {
        return gwjConfNoticeMapper.selectGwjConfNoticeById(id);
    }

    /**
     * 查询公维金信息公示列表
     *
     * @return 公维金信息公示
     */
    @Override
    public List<GwjConfNoticePageDTO> selectGwjConfNoticeList()
    {
        return gwjConfNoticeMapper.selectGwjConfNoticeList();
    }

    /**
     * 新增公维金信息公示
     * 
     * @param gwjConfNotice 公维金信息公示
     * @return 结果
     */
    @Override
    public int insertGwjConfNotice(GwjConfNotice gwjConfNotice)
    {
        gwjConfNotice.setCreateTime(DateUtils.getNowDate());
        return gwjConfNoticeMapper.insertGwjConfNotice(gwjConfNotice);
    }

    /**
     * 修改公维金信息公示
     * 
     * @param gwjConfNotice 公维金信息公示
     * @return 结果
     */
    @Override
    public int updateGwjConfNotice(GwjConfNotice gwjConfNotice)
    {
        gwjConfNotice.setUpdateTime(DateUtils.getNowDate());
        return gwjConfNoticeMapper.updateGwjConfNotice(gwjConfNotice);
    }

    /**
     * 批量删除公维金信息公示
     * 
     * @param ids 需要删除的公维金信息公示主键
     * @return 结果
     */
    @Override
    public int deleteGwjConfNoticeByIds(Long[] ids)
    {
        return gwjConfNoticeMapper.deleteGwjConfNoticeByIds(ids);
    }

    /**
     * 删除公维金信息公示信息
     * 
     * @param id 公维金信息公示主键
     * @return 结果
     */
    @Override
    public int deleteGwjConfNoticeById(Long id)
    {
        return gwjConfNoticeMapper.deleteGwjConfNoticeById(id);
    }

    /**
     * 新增公维金信息公示
     * @param saveRequest 新增参数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(GwjConfNoticeSaveRequest saveRequest) {

        // 判断业务是否已公示
        GwjConfNotice selectGwjConfigNotice = gwjConfNoticeMapper.selectGwjConfNoticeByBizId(saveRequest.getBizId());
        if(ObjectUtil.isNotEmpty(selectGwjConfigNotice)){
            throw new ServiceException("当前业务已公示请勿重复公示");
        }
        // 查询业务信息
        GwjMaintenanceFundsBusinessInner business = remoteGwjExpendInnerService.getMaintenanceFundsBusinessInfoByBizId(saveRequest.getBizId(), SecurityConstants.INNER).getData();
        if(ObjectUtil.isEmpty(business)){
            throw new ServiceException("未查询到对应业务信息");
        }
        // 判断业务类型
        if(!(business.getBizFlagId().equals(11) || business.getBizFlagId().equals(17))){
            throw new ServiceException("当前业务类型不允许信息公示");
        }
        // 拷贝参数
        GwjConfNotice gwjConfNotice = new GwjConfNotice();
        BeanUtil.copyProperties(saveRequest,gwjConfNotice);
        // 保存参数
        if(1 != gwjConfNoticeMapper.insertGwjConfNotice(gwjConfNotice)){
            throw new ServiceException("公维金信息公示新增失败");
        }
        return Boolean.TRUE;
    }

    /**
     * 判断业务是否公示
     * @param bizId 业务主键
     * @return
     */
    @Override
    public Boolean judgeExist(Long bizId) {
        // 判断业务是否已公示
        GwjConfNotice selectGwjConfigNotice = gwjConfNoticeMapper.selectGwjConfNoticeByBizId(bizId);
        if(ObjectUtil.isNotEmpty(selectGwjConfigNotice)){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
